//
//  MHSettingRedPacketViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/4.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSettingRedPacketViewController.h"
#import "MHCreatePacketTableViewCell.h"
#import "MHDetailLocationViewModel.h"
#import "MHSettingRedPacketViewModel.h"
#import "MHRedEnvelopeConfig.h"
@interface MHSettingRedPacketViewController ()
@property(nonatomic, strong) MHSettingRedPacketViewModel  *viewModel;
@end

@implementation MHSettingRedPacketViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView mh_registerNibCell:MHCreatePacketTableViewCell.class];
    self.tableView.backgroundColor = MHBGColorD8;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem mh_customItemWithTitle:@"取消" titleColor:MHWhiteTextColor imageName:nil target:self selector:@selector(pop) contentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
}

-(void) pop
{
//   RACSubject *sub = self.viewModel.finishSubject;
    [self.viewModel.finishSubject sendNext:nil];
    [self.viewModel.services popViewModelAnimated:YES];
}


- (NSString *)formatFloat:(double)f
{
    if (fmodf(f, 1)==0) {//如果有一位小数点
        return [NSString stringWithFormat:@"%.0f",f];
    } else if (fmodf(f*10, 1)==0) {//如果有两位小数点
        return [NSString stringWithFormat:@"%.1f",f];
    } else {
        return [NSString stringWithFormat:@"%.2f",f];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHCreatePacketTableViewCell *createCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(MHCreatePacketTableViewCell.class)];
    
    __weak __typeof(self)weakSelf = self;
    switch (self.viewModel.type) {
        case RedPacketNearby:
            createCell.tipsLabel.text = @"*附近列表展示，仅附近成员可领取";
            break;
        case RedPacketInCircle:
              createCell.tipsLabel.text = @"*圈内和动态展示，仅圈子内成员可领取";
            break;
        case RedPacketShare:
              createCell.tipsLabel.text = @"*圈内、动态和发现列表展示，分享到微信可领取";
            break;
        default:
            break;
    }
    if (self.viewModel.createModel.amount > 0) {
        createCell.amountTextFeild.text = [self formatFloat:self.viewModel.createModel.amount];
         createCell.numberTextFeild.text = [NSString stringWithFormat:@"%d",self.viewModel.createModel.number];
    }
    
    
//    [createCell.amountTextFeild setBk_didEndEditingBlock:^(UITextField *textFeild) {
//
//    }];
    
    if (self.viewModel.type != RedPacketNearby) {
        createCell.rangeLabel.hidden = YES;
        createCell.rangeTextField.hidden = YES;
        createCell.rangSplitLine.hidden = YES;
        createCell.rangLabelTop.constant = 0;
        createCell.remarkTopConstraint.constant = 0;
        createCell.locationBtn.hidden = YES;
//        [createCell removeConstraints:createCell.remarkImage.constraints];
//        [createCell.remarkImage removeConstraints:createCell.remarkImage.constraints];
//        [createCell.rangSplitLine removeConstraints:createCell.rangSplitLine.constraints];
//        [createCell.rangeLabel removeConstraints:createCell.rangeLabel.constraints];
//
//        [createCell.remarkImage mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(createCell.numberTextFeild.mas_bottom).offset(24);
//            make.left.equalTo(createCell.containerView).offset(15);
//            make.right.equalTo(createCell.containerView).offset(-15);
//            make.height.equalTo(@74);
//        }];
//        [createCell setNeedsUpdateConstraints];
//        [createCell.remarkImage layoutIfNeeded];
//        [createCell layoutIfNeeded];
    }else
    {
        self.viewModel.createModel.receiveRange = 5;
        createCell.rangeTextField.text = [NSString stringWithFormat:@"附近%dkm内",self.viewModel.createModel.receiveRange];
    }
    [createCell.locationBtn bk_whenTapped:^{
        MHDetailLocationViewModel *detail = [[MHDetailLocationViewModel alloc] initWithServices:weakSelf.viewModel.services params:@{MHViewModelUtilKey:weakSelf.viewModel.location,MHViewModelCommonModelUtilKey:@(true)}];
        [[detail.compoleteSubject deliverOnMainThread] subscribeNext:^(NSNumber *number  ) {
            createCell.rangeTextField.text = [NSString stringWithFormat:@"附近%dkm内",[number intValue]];
            weakSelf.viewModel.createModel.receiveRange = [number intValue];
        }];
        [weakSelf.viewModel.services pushViewModel:detail animated:YES];
    }];
    
    [createCell.amountTextFeild setBk_didEndEditingBlock:^(UITextField *textFeld) {
        YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
        MHRedEnvelopeConfig *config  =  [cache objectForKey:MHRedPacketConfig];
        weakSelf.viewModel.createModel.amount =[createCell.amountTextFeild.text doubleValue];
        if (weakSelf.viewModel.createModel.amount < ((config != nil) ? config.minTotalAmount : 1)) {
            createCell.headerTipsLabel.text = [NSString stringWithFormat:@"红包金额必须大于等于%ld元",(long)((config != nil) ? config.minTotalAmount : 1)];
//            return;
        }else
        {
            createCell.headerTipsLabel.text = @"";
        }
    }];
    
    [createCell.numberTextFeild setBk_didEndEditingBlock:^(UITextField *textFeld) {
        YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
        MHRedEnvelopeConfig *config  =  [cache objectForKey:MHRedPacketConfig];
        weakSelf.viewModel.createModel.amount =[createCell.amountTextFeild.text doubleValue];
        weakSelf.viewModel.createModel.number =[createCell.numberTextFeild.text intValue];
        CGFloat minValue = ((config != nil) ? floor(config.minAverage*100) / 100: 0.1 );
        if (weakSelf.viewModel.createModel.amount/weakSelf.viewModel.createModel.number < minValue) {
            createCell.headerTipsLabel.text = [NSString stringWithFormat:@"单个红包金额必须≥%.2f元",minValue];
//            return;
        }else
        {
             createCell.headerTipsLabel.text = @"";
        }
       
    }];
    [createCell.finishBtn bk_whenTapped:^{
        
        weakSelf.viewModel.createModel.amount =[createCell.amountTextFeild.text doubleValue];
        weakSelf.viewModel.createModel.number =[createCell.numberTextFeild.text intValue];
        
        YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
        MHRedEnvelopeConfig *config  =  [cache objectForKey:MHRedPacketConfig];
        if (weakSelf.viewModel.createModel.amount < ((config != nil) ? config.minTotalAmount : 1)) {
            [UIWindow showTips:[NSString stringWithFormat:@"红包金额必须大于等于%ld元",(long)((config != nil) ? config.minTotalAmount : 1)]];
            return;
        }
//        0.1;
        if (weakSelf.viewModel.createModel.number <= 0) {
             [UIWindow showTips:@"请填写红包个数"];
            return;
        }
        double minValue = ((config != nil) ? config.minAverage : 0.1 );
        if (weakSelf.viewModel.createModel.amount/weakSelf.viewModel.createModel.number < minValue) {
             [UIWindow showTips:[NSString stringWithFormat:@"单个红包金额必须≥%.2f元",minValue]];
            return;
        }
        
        if (createCell.reMarkTextFeild.text.length > 10) {
            [UIWindow showTips:@"最多可输入10个字"];
            return;
        }

          weakSelf.viewModel.createModel.type = weakSelf.viewModel.type;
        weakSelf.viewModel.createModel.title = createCell.reMarkTextFeild.text;
        [weakSelf.viewModel.finishSubject sendNext:weakSelf.viewModel.createModel];
        [weakSelf.viewModel.services popViewModelAnimated:YES];
    }];
    return createCell;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 500;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
    
}

@end
